package com.rit.secs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class PopupActivity extends Activity{
	
	private long time = com.rit.secs.SECS.responseWaitTime;
	public static boolean amIActive;
	private Vibrator v;
	private static TextView counterText;
	private long currentTime;
	private Dialog dialog;
	private CountDownTimer timer; 
	  
	private void createTimer(long duration) {		  
		currentTime = duration;
		timer = new CountDownTimer(duration, 1000) {
				
			public void onTick(long millisUntilFinished) {
				currentTime = millisUntilFinished;
				//System.out.println("seconds remaining: " + millisUntilFinished / 1000);
				int remainingTime = (int)(millisUntilFinished / 1000);
				counterText.setText("Remaining response time: " + remainingTime);
				if(remainingTime <= 20 && remainingTime > 10) {
					counterText.setTextColor(Color.YELLOW);
				}	
				else if(remainingTime <= 10) {
					counterText.setTextColor(Color.RED);
				}	
			}
	
			public void onFinish() {
				dialog.dismiss();
				Toast.makeText(getApplicationContext(), "Response timed out. Entry access denied.", Toast.LENGTH_LONG).show();
				finish();
			}	
		}.start();	  
	}
	  
	  public Object onRetainNonConfigurationInstance() {
		  Object [] retainList = new Object[1];
		  final long tempCounter = currentTime;
		  retainList[0] = tempCounter;
		  return retainList;
	  }
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        amIActive = true;
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.alert);
        
        counterText = (TextView) dialog.findViewById(R.id.counterText);
   	    
        
        Button grantButton = (Button) dialog.findViewById(R.id.grant);
        grantButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		ServerComm.grantAccess();
        		dialog.dismiss();
        		finish();
        	}
        });
        
        Button openButton = (Button) dialog.findViewById(R.id.open);
        openButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		Intent i = new Intent(com.rit.secs.PopupActivity.this, com.rit.secs.SECS.class);
        		i.setAction("POPUP");
        		startActivity(i);
        		System.out.println("PopupActivity: SECS has started..");
        		dialog.dismiss();
        		finish();
        		
        	}
        });
        
        Button denyButton = (Button) dialog.findViewById(R.id.deny);
        denyButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		ServerComm.denyAccess();
        		dialog.dismiss();
        		finish();
        	}
        });
        
        dialog.setTitle("SECS Alert!");
        dialog.setCancelable(false);
        dialog.show();  
        
        // Reload picture & camera on orientation change
        final Object changes = getLastNonConfigurationInstance();
        Object[] changeList = (Object[]) changes; 
        
        if(changeList != null) {
        	// changeList [0] - The time remaining in the count down timer
        	currentTime = ((Long) changeList[0]).longValue();
        	if(currentTime > 0L) {
        		createTimer(currentTime);
        	}
        }
        else {
        	createTimer(SECS.responseWaitTime);
        }
        
        v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        if(v == null) {
        	System.out.println("The vibrator is null");
        }
        else {
        	System.out.println("The vibrator is NOT null");
        	v.vibrate(500L);
        }
	}
    
    public void onNewIntent(Intent intent) {
    	System.out.println("Inside onNewIntent");
    	
    	if(intent != null) {
    		System.out.println("Action = " + intent.getAction());
    		Bundle extras = intent.getExtras();
    		if(extras !=null) {
				String reason = extras.getString("Reason");
	    		if(intent.getAction().equals("ALERT_CANCELED")) {
	    			if(timer != null) timer.cancel();
	    			dialog.dismiss();
	    			finish();
	    			Toast.makeText(getApplicationContext(), "Alert Canceled: " + reason, Toast.LENGTH_LONG).show();
	    		}
    		}
    	}
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    	dialog.dismiss();
    	if(timer != null) {
    		timer.cancel();
    	}
    }
    
    @Override
    public void finish() {
    	super.finish();
    	if(timer != null) {
    		timer.cancel();
    	}
    	dialog.dismiss();
    	amIActive = false;
    }
	
}
